<template>
  <div>
    <!-- 整个容器 -->
    <el-container>
      <!-- 上半部分：顶栏 -->
      <el-header class="layout-header">
        <div class="login-user">
          <span class="welcome">欢迎回来, {{admin.nickname}}! </span>
          <el-dropdown>
            <el-avatar size="medium" :src="admin.avatar"></el-avatar>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item icon="el-icon-s-finance" @click.native="openEditDialog()">修改资料</el-dropdown-item>
              <el-dropdown-item icon="el-icon-s-custom" @click.native="openEditAvatarDialog()">修改头像</el-dropdown-item>
              <el-dropdown-item icon="el-icon-s-cooperation" @click.native="openEditPwdDialog()">修改密码</el-dropdown-item>
              <el-dropdown-item icon="el-icon-remove" divided @click.native="logout()">退出登录</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </div>
        <h1>梅江国际车展管理系统</h1>
      </el-header>
      <!-- 下半部分 -->
      <el-container class="layout-body">
        <!-- 下半部分的左侧：侧边栏 -->
        <el-aside class="layout-aside">
          <el-menu
              router
              :default-active="activeMenuItemPath"
              class="el-menu-vertical-demo"
              background-color="#545c64"
              text-color="#fff"
              active-text-color="#fff">
            <!-- 首页 -->
            <el-menu-item index="/">
              <el-link href="/index" target="_blank" style="color: #fff">
                <i class="el-icon-s-home"></i>官网首页
              </el-link>
            </el-menu-item>
            <!-- 账户管理 -->
            <el-submenu index="2">
              <template slot="title">
                <i class="el-icon-user"></i><span>账户管理</span>
              </template>
              <el-menu-item index="/sys-admin/serve/user">
                <i class="el-icon-user"></i><span>用户列表</span>
              </el-menu-item>
            </el-submenu>
            <!-- 车展管理 -->
            <el-submenu index="3">
              <template slot="title">
                <i class="el-icon-date"></i><span>车展管理</span>
              </template>
              <el-menu-item index="/sys-admin/serve/show">
                <i class="el-icon-document-copy"></i><span>车展列表</span>
              </el-menu-item>
              <el-menu-item index="/sys-admin/serve/show/add-new">
                <i class="el-icon-circle-plus"></i><span>车展新增</span>
              </el-menu-item>
            </el-submenu>
            <!-- 票务管理 -->
            <el-submenu index="4">
              <template slot="title">
                <i class="el-icon-s-order"></i><span>购票管理</span>
              </template>
              <el-menu-item index="/sys-admin/serve/ticket-order">
                <i class="el-icon-tickets"></i><span>购票列表</span>
              </el-menu-item>
              <el-menu-item index="/sys-admin/serve/ticket-list">
                <i class="el-icon-document-delete"></i><span>退票审核</span>
              </el-menu-item>
            </el-submenu>
            <!-- 稿件管理 -->
            <el-submenu index="5">
              <template slot="title">
                <i class="el-icon-document"></i><span>稿件管理</span>
              </template>
              <el-menu-item index="5-1" disabled>
                <i class="el-icon-folder"></i><span>稿件列表</span>
              </el-menu-item>
              <el-menu-item index="5-2" disabled>
                <i class="el-icon-circle-plus"></i><span>稿件新增</span>
              </el-menu-item>
<!--              <el-menu-item index="5-3">
                <i class="el-icon-edit-outline"></i><span>展会指南维护</span>
              </el-menu-item>-->
              <el-menu-item index="/sys-admin/serve/banner">
                <i class="el-icon-picture-outline"></i><span>轮播图维护</span>
              </el-menu-item>
            </el-submenu>
            <!-- 权限管理 -->
            <el-submenu index="6">
              <template slot="title">
                <i class="el-icon-setting"></i><span>权限管理</span>
              </template>
              <el-menu-item index="/sys-admin/permission/admin">
                <i class="el-icon-user-solid"></i><span>管理员列表</span>
              </el-menu-item>
              <el-menu-item index="/sys-admin/permission/role">
                <i class="el-icon-s-custom"></i><span>角色列表</span>
              </el-menu-item>
              <el-menu-item index="/sys-admin/permission/permission">
                <i class="el-icon-s-custom"></i><span>权限列表</span>
              </el-menu-item>
            </el-submenu>
          </el-menu>
        </el-aside>
        <!-- 下半部分的右侧：主体 -->
        <el-main class="layout-main">
          <router-view/>
        </el-main>
      </el-container>
    </el-container>

    <el-dialog title="修改密码" :visible.sync="dialogPwdFormVisible"
               @closed="resetForm('editPwdForm')">
      <el-form :model="editPwdForm" :rules="rules" ref="editPwdForm" label-width="100px">
        <el-form-item label="用户名">
          <el-input v-model="admin.username" disabled></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="editPwdForm.password" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogPwdFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitEditPwdForm">确 定</el-button>
      </div>
    </el-dialog>

    <el-dialog title="修改头像" :visible.sync="dialogAvatarFormVisible"
               @closed="resetForm('editAvatarForm')">
      <el-form :model="editAvatarForm" :rules="rules" ref="editAvatarForm" label-width="100px">
        <el-form-item label="用户名">
          <el-input v-model="admin.username" disabled></el-input>
        </el-form-item>
        <el-form-item label="头像">
          <div class="picture-list">
            <el-upload
                name="imageFile"
                class="picture-uploader"
                :action="uploadAvatar"
                :show-file-list="false"
                :on-success="handleUploadSuccess">
              <i class="el-icon-upload picture-uploader-icon"></i>
              <div class="picture-uploader-text">仅允许上传 <i>JPG</i> / <i>PNG</i> 格式的图片</div>
              <div class="picture-uploader-text">仅允许上传不超过 <i>1MB</i> 的图片</div>
            </el-upload>
            <div class="picture-item">
              <el-image class="image"
                        :src="editAvatarForm.avatar" fit="cover"></el-image>
            </div>
          </div>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogAvatarFormVisible = false">关 闭</el-button>
      </div>
    </el-dialog>

    <el-dialog title="修改资料" :visible.sync="dialogFormVisible"
               @closed="resetForm('editForm')">
      <el-form :model="editForm" :rules="rules" ref="editForm" label-width="130px">
        <el-form-item label="用户名">
          <el-input v-model="admin.username" disabled></el-input>
        </el-form-item>
        <el-form-item label="昵称" prop="nickname">
          <el-input v-model="editForm.nickname" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="手机号码" prop="phone">
          <el-input v-model="editForm.phone" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="电子邮箱" prop="email">
          <el-input v-model="editForm.email" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="简介" prop="description">
          <el-input v-model="editForm.description" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitEditForm">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import global from '../Global'
let preAdminUrl = global.preAdmins ;
let preResourceUrl = global.preResources ;
export default {
  data() {
    return {
      uploadAvatar: preResourceUrl+'/upload/image/avatar',
      admin: {},
      dialogPwdFormVisible: false,
      editPwdForm: {
        password: ''
      },
      dialogAvatarFormVisible: false,
      editAvatarForm: {
        avatar: '',
        oldUrl: ''
      },
      dialogFormVisible: false,
      editForm: {
        nickname: '',
        phone: '',
        email: '',
        description: ''
      },
      activeMenuItemPath: '',
      rules: {
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 2, max: 15, message: '长度在 2 到 15 个字符', trigger: 'blur' }
        ],
        nickname: [
          { required: true, message: '请输入昵称', trigger: 'blur' },
          { min: 2, max: 25, message: '长度在 2 到 25 个字符', trigger: 'blur' }
        ],
        phone: [
          { required: true, message: '请输入手机号码', trigger: 'blur' },
          { pattern: /^1[0-9]{10}$/, message: '手机号码格式错误', trigger: 'blur' }
        ],
        email: [
          { required: true, message: '请输入邮箱', trigger: 'blur' },
          { pattern: /^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(.[a-zA-Z0-9_-]+)+$/, message: '邮箱格式错误', trigger: 'blur' }
        ],
        description: [
          { required: true, message: '请输入简介', trigger: 'blur' }
        ]
      }
    };
  },
  methods: {
    handleActiveMenuItemPath(){
      let currentPath = this.$router.currentRoute.path;
      console.log(currentPath);
      if(currentPath = '/'){
        currentPath = '';
      }else if(currentPath.startsWith('/sys-admin/serve/banner')){
        currentPath = '/sys-admin/serve/banner';
      }else if(currentPath.startsWith('/sys-admin/serve/ticket-order')){
        currentPath = '/sys-admin/serve/ticket-order';
      }else if(currentPath.startsWith('/sys-admin/permission/admin')){
        currentPath = '/sys-admin/permission/admin';
      }else if(currentPath.startsWith('/sys-admin/permission/role')){
        currentPath = '/sys-admin/permission/role';
      }else if(currentPath.startsWith('/sys-admin/permission/permission')){
        currentPath = '/sys-admin/permission/permission';
      }


      this.activeMenuItemPath = currentPath;
    },
    //登出
    logout(){
      let url = preAdminUrl + "/logout";
      let localJwt = localStorage.getItem('localJwt');
      this.axios.create({'headers': {'Authorization': localJwt}})
          .post(url).then((response) => {
        let jsonResult = response.data;
        if(jsonResult.state == 20000){
          this.$alert("退出成功", '成功', {
            confirmButtonText: '确定',
            callback: action => {
              localStorage.removeItem("localJwt");
              this.$router.push("/login");
            }
          });
        }else{
          this.$alert(jsonResult.message, '错误', {
            confirmButtonText: '确定',
            callback: action => {
            }
          });
        }
      });
    },
    //获取当事人信息
    loadLoginPrincipal(){
      let url = preAdminUrl + "/index";

      let localJwt = localStorage.getItem('localJwt');
      this.axios.create({'headers': {'Authorization': localJwt}})
          .get(url).then((response) => {
        let jsonResult = response.data;
        if(jsonResult.state == 20000){
          this.admin = jsonResult.data;
        }else{
          this.$alert(jsonResult.message, '错误', {
            confirmButtonText: '确定',
            callback: action => {
            }
          });
        }
      });
    },
    //头像修改Dialog
    openEditAvatarDialog(){
      this.editAvatarForm.avatar = this.admin.avatar;
      this.dialogAvatarFormVisible = true;
    },
    //头像上传成功
    handleUploadSuccess(response){
      this.editAvatarForm.oldUrl =  this.editAvatarForm.avatar;
      this.editAvatarForm.avatar = response.data.url;

      let url = preAdminUrl + "/" + this.admin.id + "/updateAvatar";
      let formData = this.qs.stringify(this.editAvatarForm);

      let localJwt = localStorage.getItem('localJwt');
      this.axios.create({'headers': {'Authorization': localJwt}})
          .post(url, formData).then((response) => {
        let jsonResult = response.data;
        if (jsonResult.state == 20000) {
          this.admin.avatar = this.editAvatarForm.avatar;
          this.handleDeletePicture();
        }else if(jsonResult.state == 40400){
          this.$alert(jsonResult.message, '错误', {
            confirmButtonText: '确定',
            callback: action => {
              localStorage.removeItem("localJwt");
              this.$router.push("/login");
            }
          });
        }else {
          this.$alert(jsonResult.message, '警告', {
            confirmButtonText: '确定',
            callback: action => {
            }
          });
        }
      });
    },
    //物理删除服务器保存头像
    handleDeletePicture(){
      if(this.editAvatarForm.oldUrl == ''){
        return;
      }
      let url = preResourceUrl + "/remove/image/avatar";
      let formData = this.qs.stringify(this.editAvatarForm);

      let localJwt = localStorage.getItem('localJwt');
      this.axios.create({'headers': {'Authorization': localJwt}})
          .post(url, formData).then((response) => {
        let jsonResult = response.data;
        if(jsonResult.state == 20000){
          console.log("图片物理删除成功!");
        }else{
          console.log("图片物理删除失败!");
        }
      });
    },

    //密码修改Dialog
    openEditPwdDialog(){
      this.dialogPwdFormVisible = true;
    },
    //密码修改提交
    submitEditPwdForm(){
      let url = preAdminUrl + "/" + this.admin.id + "/updatePwd";
      let formData = this.qs.stringify(this.editPwdForm);

      let localJwt = localStorage.getItem('localJwt');
      this.axios.create({'headers': {'Authorization': localJwt}})
          .post(url, formData).then((response) => {
        let jsonResult = response.data;
        if(jsonResult.state == 20000){
          this.$notify.success({title: '成功',
            message: '修改密码成功！'
          });

          this.dialogPwdFormVisible = false;
        }else if(jsonResult.state == 40400){
          this.$alert(jsonResult.message, '错误', {
            confirmButtonText: '确定',
            callback: action => {
              localStorage.removeItem("localJwt");
              this.$router.push("/login");
            }
          });
        }else{
          this.$alert(jsonResult.message, '错误', {
            confirmButtonText: '确定',
            callback: action => {
            }
          });
        }
      });
    },
    //管理员修改Dialog
    openEditDialog(){
      this.editForm = this.admin;
      this.dialogFormVisible = true;
    },
    //管理员修改
    submitEditForm(){
      let url = preAdminUrl + "/" + this.admin.id + "/update";
      let formData = this.qs.stringify(this.editForm);

      let localJwt = localStorage.getItem('localJwt');
      this.axios.create({'headers': {'Authorization': localJwt}})
          .post(url, formData).then((response) => {
        let jsonResult = response.data;
        if(jsonResult.state == 20000){
          this.$notify.success({title: '成功',
            message: '编辑资料成功！'
          });
          this.dialogFormVisible = false;
        }else if(jsonResult.state == 40400){
          this.$alert(jsonResult.message, '错误', {
            confirmButtonText: '确定',
            callback: action => {
              this.dialogFormVisible = false;
              localStorage.removeItem("localJwt");
              this.$router.push("/login");
            }
          });
        }else{
          this.$alert(jsonResult.message, '错误', {
            confirmButtonText: '确定',
            callback: action => {
            }
          });
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  },
  mounted() {
    this.handleActiveMenuItemPath();
    this.loadLoginPrincipal();
  }
}
</script>

<style>

.layout-header {
  height: 100px !important;
  background: #2c3e50;
}

.layout-header h1 {
  line-height: 100px;
  color: #fff;
  font-size: 36px;
}

.layout-header .login-user {
  float: right;
  display: flex;
  margin-top: 32px;
}

.layout-header .login-user .welcome {
  line-height: 36px;
  color: #fff;
  font-size: 20px;
  margin-right: 20px;
}


.layout-body {
  position: absolute;
  top: 100px;
  bottom: 0;
  left: 0;
  right: 0;
}

.layout-aside {
  width: 260px !important;
  background-color: #545c64;
}

.layout-aside i {
  color: #fff !important;
}

.layout-aside .el-menu-item.is-active {
  background: #2c3e50 !important;
}

.layout-aside .el-menu {
  border: 0;
}

.layout-main {
  background-color: #fff;
}


.picture-list .picture-item {
  width: 230px;
  height: 230px;
  margin-right: 20px;
  margin-bottom: 20px;
  border: 1px solid #e8e6e6;
  border-radius: 6px;
  float: left;
  display: inline;
  position: relative;
}

.picture-list .picture-item .cover-flag {
  position: absolute;
  left: 0;
  top: 0;
  z-index: 1;
  border-top-left-radius: 6px;
}

.picture-list .picture-item .cover-flag .button {
  background: transparent;
  border: none;
  border-top-left-radius: 6px;
  font-size: 20px;
  padding: 8px;
}

.picture-list .picture-item .cover-flag .button:hover {
  background: #fcefef;
}

.picture-list .picture-item .cover-flag .cover {
  color: #f00;
}

.picture-list .picture-item .cover-flag .not-cover {
  color: #aaa;
}

.picture-list .picture-item .delete-button {
  position: absolute;
  right: 0;
  bottom: 0;
  z-index: 1;
  border-bottom-right-radius: 6px;
}

.picture-list .picture-item .delete-button .button {
  border: none;
  border-bottom-right-radius: 6px;
}

.picture-list .picture-item .image {
  width: 230px;
  height: 230px;
  border-radius: 6px;
}

.picture-list .picture-uploader {
  float: left;
  width: 230px;
  height: 230px;
  margin-right: 20px;
  margin-bottom: 20px;
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  margin-bottom: 20px;
}

.picture-list .picture-uploader .picture-uploader-icon {
  width: 230px;
  height: 150px;
  font-size: 58px;
  color: #8c939d;
  line-height: 150px;
  text-align: center;
}

.picture-list .picture-uploader .picture-uploader-text {
  width: 230px;
  height: 24px;
  line-height: 24px;
  text-align: center;
  font-size: 12px;
  color: #8c939d;
}

.picture-list .picture-uploader .picture-uploader-text i {
  font-size: 12px;
  font-weight: bold;
  font-style: normal;
  color: #F56C6C;
}
</style>





